package com.xianye.erp.service.order.impl;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AliexpressTradeNewRedefiningFindorderbyidRequest;
import com.taobao.api.request.AliexpressTradeNewRedefiningFindorderbyidRequest.AeopTpSingleOrderQuery;
import com.taobao.api.request.AliexpressTradeRedefiningFindorderlistqueryRequest;
import com.taobao.api.request.AliexpressTradeSellerOrderlistGetRequest;
import com.taobao.api.request.AliexpressTradeSellerOrderlistGetRequest.AeopOrderQuery;
import com.taobao.api.response.AliexpressTradeNewRedefiningFindorderbyidResponse;
import com.taobao.api.response.AliexpressTradeRedefiningFindorderlistqueryResponse;
import com.taobao.api.response.AliexpressTradeSellerOrderlistGetResponse;
import com.xianye.erp.factory.ApiFacade;
import com.xianye.erp.util.PageData;
@Service("expressOrderAPI")
public class ExpressOrderAPI {
    protected Logger log = Logger.getLogger(ExpressOrderAPI.class);

    @Autowired
    private ApiFacade facade;

 /*   public JSONObject getOrderList(PageData store, Long page,
                                   Long pageSize, String createDateStart,
                                   String createDateEnd, String orderStatus){
        try {
            TaobaoClient client = new DefaultTaobaoClient(facade.getUrl(), facade.getAppkey(), facade.getSecret());
            AliexpressTradeRedefiningFindorderlistqueryRequest req = new AliexpressTradeRedefiningFindorderlistqueryRequest();
            AliexpressTradeRedefiningFindorderlistqueryRequest.OrderListRequest obj1 = new AliexpressTradeRedefiningFindorderlistqueryRequest.OrderListRequest();
            obj1.setCreateDateEnd(createDateEnd);
            obj1.setCreateDateStart(createDateStart);
            //obj1.setOrderStatusList(orderStatus);
            obj1.setPage(page);
            obj1.setPageSize(pageSize);
            obj1.setOrderStatus(orderStatus);
            req.setParam1(obj1);
            AliexpressTradeRedefiningFindorderlistqueryResponse rsp = client.execute(req, store.getString("access_token"));
            //System.out.println(rsp.getBody());
            JSONObject res = JSONObject.fromObject(rsp.getBody());
            if(res.containsKey("aliexpress_trade_redefining_findorderlistquery_response")){
                JSONObject resp = res.getJSONObject("aliexpress_trade_redefining_findorderlistquery_response");
                if(resp.containsKey("result")){
                    return resp.getJSONObject("result");
                }
            }
            return null;
        } catch (Exception e) {
            log.error("获取订单列表发生异常",e);
            e.printStackTrace();
            return null;
        }
    }*/

    public JSONObject getOrderList(PageData store, Long page,
            Long pageSize, String createDateStart,
            String createDateEnd, String orderStatus){
		try {
			TaobaoClient client = new DefaultTaobaoClient(facade.getUrl(), facade.getAppkey(), facade.getSecret());
			AliexpressTradeSellerOrderlistGetRequest req = new AliexpressTradeSellerOrderlistGetRequest();
			AeopOrderQuery obj1 = new AeopOrderQuery();
			obj1.setCreateDateEnd(createDateEnd);
			obj1.setCreateDateStart(createDateStart);
			obj1.setCurrentPage(page);
			obj1.setPageSize(pageSize);
			obj1.setOrderStatus(orderStatus);
			req.setParamAeopOrderQuery(obj1);
			AliexpressTradeSellerOrderlistGetResponse  rsp = client.execute(req, store.getString("access_token"));
			//System.out.println(rsp.getBody());
			JSONObject res = JSONObject.fromObject(rsp.getBody());
			if(res.containsKey("aliexpress_trade_seller_orderlist_get_response")){
				JSONObject resp = res.getJSONObject("aliexpress_trade_seller_orderlist_get_response");
				if(resp.containsKey("result")){
				return resp.getJSONObject("result");
				}
			}
			return null;
		} catch (Exception e) {
			log.error("获取订单列表发生异常",e);
			e.printStackTrace();
			return null;
		}
}


    public JSONObject getJSONOrderDetail(PageData store,Long orderId){
        try {
            TaobaoClient client = new DefaultTaobaoClient(facade.getUrl(), facade.getAppkey(), facade.getSecret());
            AliexpressTradeNewRedefiningFindorderbyidRequest req = new AliexpressTradeNewRedefiningFindorderbyidRequest();
            AeopTpSingleOrderQuery  obj1 = new AeopTpSingleOrderQuery();
            obj1.setOrderId(orderId);
            req.setParam1(obj1);
            AliexpressTradeNewRedefiningFindorderbyidResponse  rsp = client.execute(req, store.getString("access_token"));
            //System.out.println(rsp.getBody());
            JSONObject res = JSONObject.fromObject(rsp.getBody());
            if(res.containsKey("aliexpress_trade_new_redefining_findorderbyid_response")){
                JSONObject resp = res.getJSONObject("aliexpress_trade_new_redefining_findorderbyid_response");
                if(resp.containsKey("target")){
                    //System.out.println("数据1"+resp.getJSONObject("target").toString());
                    return resp.getJSONObject("target");

                }
            }
            return null;
        } catch (Exception e) {
            log.error("获取订单详情发生异常",e);
            e.printStackTrace();
            return null;
        }

    }
}
